package eu.dominicum.ft.pd;

import eu.dominicum.ft.pd.mao.AssociateLocationsWithStageMAO;
import eu.dominicum.ft.pd.mao.MakeStageMAO;
import eu.dominicum.ft.pd.mao.MakeStageResultMAO;

import java.util.ArrayList;
import java.util.List;

/**
 * A stage in a Tour.
 *
 * @author Harry Klerks
 * @since 2013M1
 */
public class Stage extends PersistentDomainObject {

    /**
     * Sequence number.
     */
    private Integer sequence;

    /**
     * The associated {@link Tour}.
     */
    private Tour tour;

    /**
     * The associated {@link StageResult} objects.
     */
    private final List<StageResult> stageResults = new ArrayList<>();

    /**
     * From location.
     */
    private Location fromLocation;

    /**
     * To location.
     */
    private Location toLocation;

    /**
     * Make a Stage object.
     *
     * @param makeStageMAO -
     * @return Stage
     */
    public static Stage make(final MakeStageMAO makeStageMAO) {

        Stage newStage = new Stage();

        newStage.sequence = makeStageMAO.getSequence();
        newStage.tour = makeStageMAO.getTour();

        return newStage;
    }

    /**
     * A protected constructor to satisfy JPA.
     */
    protected Stage() {

    }

    @Override
    public final String toString() {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(this.sequence);

        return stringBuilder.toString();
    }

    /**
     * .
     *
     * @return Integer
     */
    public final Integer getSequence() {
        return sequence;
    }

    /**
     * .
     *
     * @param neSequence -
     */
    public final void setSequence(final Integer neSequence) {
        this.sequence = neSequence;
    }

    /**
     * .
     *
     * @return Tour
     */
    public final Tour getTour() {
        return tour;
    }

    /**
     * .
     *
     * @return List<StageResult>
     */
    public final List<StageResult> getStageResults() {
        return stageResults;
    }

    /**
     * Make and add a StageResult object.
     *
     * @param makeStageResultMAO -
     * @return StageResult
     */
    public final StageResult makeAndAddStageResult(
            final MakeStageResultMAO makeStageResultMAO) {

        StageResult newStageResult = StageResult.make(makeStageResultMAO);
        this.stageResults.add(newStageResult);

        return newStageResult;
    }

    /**
     * .
     *
     * @return Location
     */
    public final Location getFromLocation() {
        return fromLocation;
    }

    /**
     * .
     *
     * @return Location
     */
    public final Location getToLocation() {
        return toLocation;
    }

    /**
     * Associate given {@link Location} object(s) with this Stage.
     *
     * @param associateLocationsWithStageMAO -
     */
    public final void associateLocations(final AssociateLocationsWithStageMAO associateLocationsWithStageMAO) {

        if (associateLocationsWithStageMAO.getFromLocation() != null) {
            this.fromLocation = associateLocationsWithStageMAO.getFromLocation();
        }
        if (associateLocationsWithStageMAO.getToLocation() != null) {
            this.toLocation = associateLocationsWithStageMAO.getToLocation();
        }
    }
}
